package com.shixulin.controller;


import com.shixulin.domain.ResponseResult;
import com.shixulin.entity.Course;
import com.shixulin.entity.DTO.CourseQueryDTO;
import com.shixulin.entity.filess;
import com.shixulin.mapper.CourseInfoMapper;
import com.shixulin.oss.handleBigFile;
import com.shixulin.oss.handleFile;
import com.shixulin.service.CourseInfoService;
import com.shixulin.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/teacher")
@CrossOrigin
public class CourseController {
    @Autowired
    private handleFile handle;

    @Autowired
    private CourseService courseService;

    @Autowired
    private handleBigFile handleBigFile;


    @PostMapping("/handleFile")
    public ResponseResult handleFileUpload(
            @RequestParam("teacher_id") String teacher_id,
            @RequestParam("subject_id") String subject_id,
            @RequestParam("text") String text,
            @RequestParam("status") String status,
            @RequestParam("filename") MultipartFile file) throws IOException {

        if (file != null && !file.isEmpty()) {
            String fileName =  file.getOriginalFilename();
            filess field = new filess();
//            String url =  handle.uploadFile(file);
            long fileSize = file.getSize();
            // 将文件大小转换为KB
            double fileSizeKB = fileSize / 1024.0;
            //当小于1M时，使用直接上传方式
            if(fileSizeKB<1000){
                System.out.println("处理小文件");
                field = handle.uploadFile(file);
                if (field == null) {
                    return new ResponseResult(405,"失败");
                }
            }else { //否则使用分片方式上传
                System.out.println("处理大文件");
                field = handleBigFile.fileUploadZone(file);
                if (field == null) {
                    return new ResponseResult(405,"失败");
                }
            }
            String url = field.getUrl();
            String alyfilename = field.getAlyfilename();
            float size = file.getSize();
            Course course = new Course(null,Integer.parseInt(subject_id),Integer.parseInt(teacher_id),text,fileName,alyfilename,size,url,Integer.parseInt(status)  );
            return courseService.insertCourseInfo(course);
        }else {
            return new ResponseResult(501,"文件不能为空");
        }
    }

//    @GetMapping("/getCourseInfo/{id}")
//    public ResponseResult getCourseInfo(@PathVariable("id") int id){
//        return courseService.getCourseInfo(id);
//    }
    @Autowired
     private CourseInfoService courseInfoService;

    @PostMapping("/getCourseInfo")
    public ResponseResult getCourseInfo(@RequestBody CourseQueryDTO courseQueryDTO  ){

        return courseInfoService.getCourseInfoByPage(courseQueryDTO);
    }


    @PostMapping("/changeCourseInfo")
    public ResponseResult changeCourseInfo(
            @RequestParam("id") String id,
            @RequestParam("subject_id") String subjectId,
            @RequestParam("filename")String filename,
            @RequestParam("text") String text,
            @RequestParam("status") String status){
        Course course = new Course(Integer.parseInt(id),Integer.parseInt(subjectId) ,text,Integer.parseInt(status),filename );
        return courseService.changeCourseInfo(course);
    }

    @GetMapping("/deleteCourseInfo/{id}")
    public ResponseResult deleteCourseInfo(@PathVariable("id") String id){
        //获取在服务器上的名字
        String alyfilename = courseService.getalyFilename(id);
        if(alyfilename != null){
            //去删除服务器上的文件
            boolean flag = handle.deleteFile(alyfilename);
            if(flag){
                return courseService.deleteCourseInfo(Integer.parseInt(id));
            }else {
                return new ResponseResult<>(404,"服务器删除失败");
            }
        }
        return new ResponseResult(404,"服务器删除失败");
    }
}
